


//
//  DoorSystemCheckViewController.m
//  RONGAO
//
//  Created by ZhangYeKun on 2017/2/24.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "DoorSystemCheckViewController.h"
#import "DoorSystemCheckTableViewCell.h"
#import "PhotoViewController.h"
#import "VideoListViewController.h"


@interface DoorSystemCheckViewController ()<UITableViewDelegate,UITableViewDataSource>
/** tableView **/
@property(nonatomic,strong) UITableView *tableView;
/** 配置文件 **/
@property(nonatomic,strong) NSArray *settingPlist;

/** directoryPath **/
@property(nonatomic,strong) NSString *directoryPath;
@end

@implementation DoorSystemCheckViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self setNavigationStyle];

}


#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.settingPlist.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.settingPlist[section] count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DoorSystemCheckTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCheckTableViewCellIdentifier];
    if (cell == nil) {
        
        cell = [[DoorSystemCheckTableViewCell alloc]init];
        
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setupViewWithConfig:self.settingPlist[indexPath.section][indexPath.row]];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return tableView.bounds.size.height/2 - 2;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 2;
    
}
#pragma mark - --------------------------UITableView delegate--------------------------

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([AppDelegate globalDelegate].cameraModel != nil) {
        
        
        if (PHOTO_LOCAL_SOURCE_TYPE == indexPath.section ) {
            
            if ([self dataSourceNum:[AppDelegate globalDelegate].cameraModel.uid type:indexPath.section]) {
            } else {
                return [SVProgressHUD showErrorWithStatus:@"没有图片"];
            }
            
            PhotoViewController *vc = [PhotoViewController new];
            vc.directoryPath = self.directoryPath;
            vc.localSourceType = indexPath.section;
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            self.hidesBottomBarWhenPushed = NO;

        }
        if (indexPath.section == 1) {
            
            VideoListViewController *vc = [VideoListViewController new];
            vc.directoryPath = self.directoryPath;
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            self.hidesBottomBarWhenPushed = NO;
            
        }
        
    }
    
    
}
- (NSInteger)dataSourceNum:(NSString *)uid type:(NSInteger)type
{
    NSInteger num = 0;
    
    if (PHOTO_LOCAL_SOURCE_TYPE == type) {
        FMResultSet *rs = [[AppDelegate globalDelegate].database executeQuery:@"SELECT * FROM snapshot WHERE dev_uid=?", uid];
        while([rs next]) {
            ++num;
        }
        [rs close];
        
    } else if(VIDEO_LOCAL_SOURCE_TYPE == type) {
        FMResultSet *rs = [[AppDelegate globalDelegate].database executeQuery:@"SELECT * FROM record WHERE dev_uid=?", uid];
        while([rs next]) {
            ++num;
        }
        [rs close];
    }
    //NSLog(@"uid:%@   num:%d", uid, num);
    return num;
}

- (NSString *)directoryPath {
    
    if (!_directoryPath) {
        
        NSArray* dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        _directoryPath = [dirs objectAtIndex:0];
    }
    return _directoryPath;
}


- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    
    return _tableView;
    
}

- (NSArray *)settingPlist{
    
    if (!_settingPlist) {
        
        _settingPlist = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DoorSystemCheckSetting" ofType:@"plist"]];
        
    }
    
    return _settingPlist;
    
}

- (void)setNavigationStyle{
    
    [self setNavigationTitle:@"查看" titleColor:[UIColor colorWithRed:210/255.0 green:177/255.0 blue:104/255.0 alpha:1]];

    
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.left.right.mas_equalTo(superView);
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-TabBarHeight);
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
